package com.lgx.common.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.dao.IIntraCityDao;
import com.lgx.common.entity.IntraCity;
import com.lgx.common.exception.DataException;

@Component
public class IntraCityDao extends GeneralDao implements IIntraCityDao {
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	@Override
	@Transactional
	public IntraCity createIntraCity(IntraCity city) throws DataException {
		this.getHibernateTemplate().save(city);
		log.info("[创建同城]:"+city.getId());
		return city;
	}

	@Override
	@Transactional
	public IntraCity updateIntraCity(IntraCity city) throws DataException {
		this.getHibernateTemplate().update(city);
		log.info("[更新同城]:"+city.getId());
		return city;
	}

	@Override
	@Transactional
	public void deleteIntraCity(IntraCity city) throws DataException {
		log.info("[删除同城]:"+city.getId());
		this.getHibernateTemplate().delete(city);
	}

	@Override
	public void deleteIntraCity(String id) throws DataException {
		this.deleteIntraCity(this.findIntraCityById(id));
	}

	@Override
	public IntraCity findIntraCityById(String id) {
		return this.getHibernateTemplate().get(IntraCity.class, Long.parseLong(id));
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public IntraCity findIntraCityByAreaName(String areaName){
		String hql = "from IntraCity i where i.areaName = '"+areaName+"' order by i.createDate desc";
		List<IntraCity> list = this.getHibernateTemplate().find(hql);
		if(list.size() != 0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IntraCity> findIntraCityList() {
		String hql = "from IntraCity i order by i.createDate asc";
		return this.getHibernateTemplate().find(hql);
	}

}
